package com.jstyle.jclifexd.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.NetResultData;

import com.jstyle.jclifexd.utils.NetWorkConast;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.view.EditTextWithDelete;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class UpdateEmailActivity extends BaseActivity {

    @BindView(R.id.tv_forgotpwd_title)
    TextView tvForgotpwdTitle;
    @BindView(R.id.tv_register_tips)
    TextView tvRegisterTips;
    @BindView(R.id.edit_forgotpwd_email)
    EditTextWithDelete editForgotpwdEmail;
    @BindView(R.id.bt_forgotpwd_back)
    Button btForgotpwdBack;
    @BindView(R.id.bt_reset_pwd)
    Button btResetPwd;
    @BindView(R.id.tv_email_alert)
    TextView tvEmailAlert;
    @BindView(R.id.tv_resend_email)
    TextView tvResendEmail;
    private Disposable resetDisposable;
    private Disposable activateDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_email);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        tvResendEmail.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);
    }

    @OnClick({R.id.bt_forgotpwd_back, R.id.bt_reset_pwd, R.id.tv_resend_email})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_forgotpwd_back:
                finish();
                break;
            case R.id.bt_reset_pwd:
                toRestPwd();
                break;
            case R.id.tv_resend_email:
                resendEmail();
                break;
        }
    }

    private void toRestPwd() {
        String email=editForgotpwdEmail.getText().toString();
        if(TextUtils.isEmpty(email)){
            showToast(getString(R.string.fill_all_info));
            return;
        }
        if (!NetWorkUtil.isNetworkConnected(this)) {
            showToast(getString(R.string.Network_not_availa));
            return;
        }
        String uid=NetWorkUtil.getUserId();
        Map<String, Object> map = new HashMap<>();
        map.put(NetWorkConast.KEY_userEmail, email);
        map.put(NetWorkConast.KEY_uid, uid);

        NetWorkUtil.addSHA1(map);
        NetWorkUtil.getInstance().getJstyleApi().updateEmail(map).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                resetDisposable =disposable;
                showProgressDialog(getString(R.string.smssdk_wait));
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NetResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NetResultData netResultData) {
                disMissProgressDialog();
                int msgCode = netResultData.getMsgCode();
                String msg = NetWorkConast.getResult(msgCode, UpdateEmailActivity.this);
                if (msgCode == NetResultData.Success_Code) {
                    ScreenUtils.showSetSuccessFul(editForgotpwdEmail,getString(R.string.Update_email_Successful));

                    //   msg = getString(R.string.login_complete);
                   // sendEmailSuccessful();
                }else{
                    showToast(msg);
                }
                //
            }

            @Override
            public void onError(Throwable e) {
                disMissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(resetDisposable);
        unSubscribe(activateDisposable);
    }
    @Override
    protected void ActivateFinished() {
        super.ActivateFinished();
        tvEmailAlert.setVisibility(View.GONE);
        tvResendEmail.setVisibility(View.GONE);
    }

    @Override
    protected void ActivateUnFinished() {
        super.ActivateUnFinished();
        tvEmailAlert.setVisibility(View.VISIBLE);
        tvResendEmail.setVisibility(View.VISIBLE);
    }

    private void resendEmail() {
        String uid= NetWorkUtil.getUserId();



        NetWorkUtil.getInstance().getJstyleApi().activateEmail(uid).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                activateDisposable =disposable;
                showProgressDialog(getString(R.string.smssdk_wait));
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NetResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NetResultData netResultData) {
                disMissProgressDialog();
                int msgCode = netResultData.getMsgCode();
                String msg = NetWorkConast.getResult(msgCode, UpdateEmailActivity.this);
                if (msgCode == NetResultData.Success_Code) {
                    showToast(getString(R.string.resend_email_success));
                    //   msg = getString(R.string.login_complete);
                    // sendEmailSuccessful();
                }else{
                    showToast(msg);
                }
                //
            }

            @Override
            public void onError(Throwable e) {
                disMissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

}
